﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;

namespace OrderTest
{
    [Serializable]
    public class OrderDetails
    {
        double price;
        int amount;
        string productName;
        double totalPrice;

        public double getTotalPrice()
        {
            return totalPrice;
        }
        public void setPrice(double price)
        {
            this.price = price;
        }
        public double getPrice()
        {
            return price;
        }
        public void setAmount(int amount)
        {
            this.amount = amount;
        }
        public int getAmount()
        {
            return amount;
        }
        public void setProductName(string productName)
        {
            this.productName = productName;
        }
        public string getProductName()
        {
            return productName;
        }

        public override bool Equals(object obj)
        {
            return obj is OrderDetails details &&
                   price == details.price &&
                   amount == details.amount &&
                   productName == details.productName &&
                   totalPrice == details.totalPrice;
        }


        public OrderDetails(double price, int amount, string productName)
        {
            setAmount(amount);
            setPrice(price);
            setProductName(productName);
            totalPrice = this.price * this.amount;
        }

        public OrderDetails()
        {
        }

        public override string ToString()
        {
            return ("商品名称：" + productName + "  单价：" + price.ToString() + "  数量：" + amount.ToString());
        }

        public override int GetHashCode()
        {
            int hashCode = -919010476;
            hashCode = hashCode * -1521134295 + price.GetHashCode();
            hashCode = hashCode * -1521134295 + amount.GetHashCode();
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(productName);
            hashCode = hashCode * -1521134295 + totalPrice.GetHashCode();
            return hashCode;
        }
    }
    [Serializable]
    public class Order
    {
        string orderID;
        string client;
        double totalPrice = 0;
        List<OrderDetails> details = new List<OrderDetails>();

        public void deleteDetails(OrderDetails detail)
        {
            details.Remove(detail);
            foreach (OrderDetails details in this.details)
            {
                totalPrice += details.getTotalPrice();
            }
        }

        public void addDetails(OrderDetails detail)
        {
            details.Add(detail);
            foreach (OrderDetails details in this.details)
            {
                totalPrice += details.getTotalPrice();
            }
        }

        public string getOrderID()
        {
            return orderID;
        }
        public double getTotalPrice()
        {
            return totalPrice;
        }
        public void setTotalPrice(double totalPrice)
        {
            this.totalPrice = totalPrice;
        }
        public void setClient(string client)
        {
            this.client = client;
        }
        public string getClient()
        {
            return client;
        }
        public void setDetails(List<OrderDetails> details)
        {
            this.details = details;
        }
        public List<OrderDetails> getDetails()
        {
            return details;
        }

        public override bool Equals(object obj)
        {
            return obj is Order order &&
                   orderID == order.orderID &&
                   client == order.client &&
                   totalPrice == order.totalPrice &&
                   EqualityComparer<List<OrderDetails>>.Default.Equals(details, order.details);
        }


        public Order(string orderID, string client, List<OrderDetails> details)
        {
            setClient(client);
            setDetails(details);
            this.orderID = orderID;
            foreach (OrderDetails detail in this.details)
            {
                totalPrice += detail.getTotalPrice();
            }
        }

        public Order()
        {
        }

        public override string ToString()
        {
            return ("订单号：" + orderID.ToString() + "  客户名：" + client + "总价格：" + getTotalPrice().ToString());
        }

        public override int GetHashCode()
        {
            int hashCode = 2070857855;
            hashCode = hashCode * -1521134295 + orderID.GetHashCode();
            hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(client);
            hashCode = hashCode * -1521134295 + totalPrice.GetHashCode();
            hashCode = hashCode * -1521134295 + EqualityComparer<List<OrderDetails>>.Default.GetHashCode(details);
            return hashCode;
        }
    }

    public class OrderService
    {
        List<Order> orderList = new List<Order>();

        public OrderService(Order[] orders)
        {
            orderList.Clear();
            foreach(Order order in orders)
                orderList.Add(order);
        }

        public OrderService()
        {
        }

        public void Export()
        {
            Order[] array = orderList.ToArray();
            Console.WriteLine("serilized into an XML file");
            XmlSerializer xmlserializer = new XmlSerializer(typeof(Order[]));
            using (FileStream fs = new FileStream("s.xml", FileMode.Create))
            {
                xmlserializer.Serialize(fs, array);
            }
            Console.WriteLine(File.ReadAllText("s.xml"));
        }

        public void Import(Order[] order)
        {
            Console.WriteLine("deserialized from an XML file");
            XmlSerializer xmlserializer = new XmlSerializer(typeof(Order[]));
            using (FileStream fs = new FileStream("s.xml", FileMode.Open))
            {
                order = (Order[])xmlserializer.Deserialize(fs);
                Console.WriteLine("already deserialized");
            }
        }
        public List<Order> getOrderList()
        {
            return orderList;
        }
        public void addOrder(string orderID, string client, List<OrderDetails> details)
        {
            Order order = new Order(orderID, client, details);
            orderList.Add(order);
        }
        public bool searchOrder(Order order, string orderID)
        {
            bool ifExist = false;
            foreach (Order orders in orderList)
            {
                if (orders.getOrderID() == orderID)
                {
                    order = orders;
                    ifExist = true;
                }
            }
            return ifExist;
        }
        public void deleteOrder(string orderID)
        {
            Order order = new Order();
            if (searchOrder(order, orderID))
                orderList.Remove(order);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
